package com.jsbj.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysHomepageBase;
import com.jsbj.user.mapper.SysHomepageBaseMapper;
import com.jsbj.user.service.ISysHomepageBaseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页配置表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-07-17
 */
@Service
public class SysHomepageBaseServiceImpl extends ServiceImpl<SysHomepageBaseMapper, SysHomepageBase> implements ISysHomepageBaseService {

    @Override
    public List<Map<String,Object>> getInitByCondition(String orgCode, Integer type) {
        List<Map<String,Object>> resultList = new ArrayList<>();

        LambdaQueryWrapper<SysHomepageBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysHomepageBase::getDisabled,0);
        wrapper.eq(SysHomepageBase::getType,type);
        if(type==1 || type == 2){
            List<SysHomepageBase> list = baseMapper.selectList(wrapper);
            Map<String,Object> resultMap = new HashMap<>();
            if(type==1){
                resultMap.put("title","集团首页");
                resultMap.put("id","multiple");
            }else if(type==2){
                resultMap.put("title","项目首页");
                resultMap.put("id","single");
            }
            resultMap.put("dataList",list);
            resultList.add(resultMap);
        }else if(type==3){
            //查询所有系统
            List<String> sysCodes = baseMapper.queryDistinctSysCode();
            if(sysCodes!=null && sysCodes.size()>0){
                for (String sysCode:sysCodes){
                    wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(SysHomepageBase::getDisabled,0);
                    wrapper.eq(SysHomepageBase::getType,type);
                    wrapper.eq(SysHomepageBase::getSystemCode,sysCode);
                    List<SysHomepageBase> list = baseMapper.selectList(wrapper);
                    if(list!=null && list.size()>0){
                        Map<String,Object> resultMap = new HashMap<>();
                        resultMap.put("id",sysCode);
                        resultMap.put("title",list.get(0).getSystemName());
                        resultMap.put("dataList",list);
                        resultList.add(resultMap);
                    }
                }
            }
        }
        return resultList;
    }

    @Override
    public List<SysHomepageBase> getInitData(String sysCode,Integer type) {
        LambdaQueryWrapper<SysHomepageBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysHomepageBase::getDisabled,0);
        wrapper.eq(SysHomepageBase::getType,type);
        if(StringUtils.isNotEmpty(sysCode)){
            wrapper.eq(SysHomepageBase::getSystemCode,sysCode);
        }
        return baseMapper.selectList(wrapper);
    }
}
